<!-- 后结算模块-财务管理-销售管理-开票详情 -->
<template>
  <div class="invoice-list-box">
    <el-container class="partDetails is-vertical">
      <el-header
        height="auto"
        class="no-padding">
        <record-info
          class="common-ui"
          :record-form="invoiceCommonForm"
          :record-data="headerInfo" />
      </el-header>
      <div class="product-info-box common-ui">
        <p>销售产品总数：<span>{{ headerInfo.prodTotalCount }}</span></p>
        <p>品种数：<span>{{ headerInfo.prodVarietyCount }}</span></p>
        <p>金额合计（元）：<span style="color:red;">{{ headerInfo.orderTotalAmount | getFormatterPrice }}</span></p>
      </div>
      <el-editable
        class="distributor-edit-box"
        :data="invoiceList"
        height="400"
        v-loading="tableLoading"
        :columns="invoiceTableColumns"
      >
        <!-- 发票详情 -->
        <template
          slot="default-invoiceDetails"
          slot-scope="scope">
          <el-button
            v-if="scope.row.invoiceNo"
            @click="toLookDetails(scope.row)"
            type="text">查看</el-button>
        </template>
        <!-- 发票影像 -->
        <template
          slot="default-invoiceUrl"
          slot-scope="scope">
          <div
            v-if="scope.row.invoiceUrl"
            style="display:flex; justify-content: center;">
            <el-image
              @click="toLookDetails(scope.row)"
              style="width: 23px; height: 16.6px;"
              :src="imageicon"/>
          </div>
        </template>
        <template
          slot="default-pssRegistNo"
          slot-scope="scope">
          <el-button
            :disabled="!scope.row.pssRegistNo||!scope.row.pssRegistId"
            v-html="regisNoButton(scope.row, 'pssRegist')"
            @click="showPdfRegistion(scope.row.pssRegistId)"
            type="text"
          />
        </template>
        <template
          slot="default-hosRegistNo"
          slot-scope="scope">
          <el-button
            :disabled="!scope.row.hosRegistNo||!scope.row.hosRegistId"
            v-html="regisNoButton(scope.row, 'hosRegist')"
            @click="showPdfRegistion(scope.row.hosRegistId)"
            type="text"
          />
        </template>
      </el-editable>
      <!-- 分页模块 -->
      <pagination
        ref="page"
        :request-func="getList"
        @returnData="getTableData"
      />
      <invoice-details
        :display-or-not="displayOrNotDetails"
        :invoice-url="activeInvoiceUrl"
        :busness-type="1"
        :invoice-no="activeInvoiceNo"
        @cancelDialog="cancelDialog" />
    </el-container>
  </div>
</template>

<script>
import {
  statusArr,
  invoiceTableColumns,
  invoiceCommonForm
} from './const'
import Pagination from '@components/custom-pagination'
import RecordInfo from '@/moduleComponents/beforeSettle/distributorSide/purchasingManagement/record'
import {
  distributorInvoiceById, // 配送商-后结算-开票管理-详情
  distributorInvoiceItemById, // 配送商-后结算-开票管理-详情-明细(国科系公司)
  yySettleInvoiceOrderHasInvoiceOrderItemInfo // 配送商-后结算-开票管理-详情-明细(非国科系公司)
} from '@const/beforeSettleApi'
import ElEditable from '@components/el-editable'
import invoiceDetails from '@/moduleComponents/beforeSettle/invoiceDetails/index.vue'
import { cloneDeep, isObject, hasIn } from 'lodash'
import { getFormatterPrice } from '@const/filter'
import { regisNoButton, showPdfRegistion } from '@const/agencySaas'
// 明细详情地址
const detailRequestData = {
  GOUKE: distributorInvoiceItemById,
  UNGOUKE: yySettleInvoiceOrderHasInvoiceOrderItemInfo
}
export default {
  components: { ElEditable, Pagination, RecordInfo, invoiceDetails },
  data () {
    return {
      imageicon: require('@assets/icon/beforeSettle/imageicon.png'),
      id: this.$route.query.id,
      orderStatus: null,
      // 表格Loading
      tableLoading: false,
      // 表单 详情展示内容-table
      invoiceList: [],
      invoiceTableColumns,
      // 订单详情信息
      headerInfo: {},
      invoiceCommonForm,
      // 查看发票详情
      displayOrNotDetails: false,
      activeInvoiceUrl: '',
      activeInvoiceNo: ''
    }
  },
  computed: {
    type () {
      return this.$route.query.type
    }
  },
  mounted () {
    this.getBaseList()
  },
  filters: {
    getFormatterPrice
  },
  methods: {
    regisNoButton,
    showPdfRegistion,
    // 查看发票详情
    toLookDetails (val) {
      this.activeInvoiceUrl = val.invoiceUrl
      this.activeInvoiceNo = val.invoiceNo
      this.displayOrNotDetails = true
    },
    cancelDialog () {
      this.activeInvoiceUrl = ''
      this.activeInvoiceNo = ''
      this.displayOrNotDetails = false
      this.$refs.page.Refresh()
    },
    // 列表请求方法/接口两个(国科系公司/非国科系公司)
    getList () {
      this.tableLoading = true
      // 地址选择
      const requestUrl = detailRequestData[this.type]
      const body = {
        orderId: this.id,
        ...this.$refs.page.page
      }
      return this.$axios.$post(requestUrl, body)
    },
    // 列表请求方法
    getTableData (data) {
      this.invoiceList = data
      this.tableLoading = false
    },
    // 列表请求方法
    async getBaseList () {
      const body = {
        invoiceId: this.id
      }
      const data = await this.$axios.$post(distributorInvoiceById, body)
      const baseInfo = hasIn(data, 'payload') ? data.payload : {}
      Object.keys(baseInfo).forEach(key => {
        if (key === 'orderStatus') {
          this.orderStatus = baseInfo[key]
          baseInfo[key] = statusArr[baseInfo[key]]
        }
        this.$set(this.headerInfo, key, baseInfo[key] || '--')
      })
    }
  }
}
</script>

<style lang="less" scoped>
@import '../m-front-common/pc/assets/var.less';
.invoice-list-box {
  background: @bgColor;
  height: 100%;
  .no-padding.el-header {
    padding: 0;
  }
  .common-ui {
    padding: 8px 0;
    background: #fff;
    margin-bottom: 8px;
  }
  .partDetails {
    height: 100%;
    .product-info-box {
      display: flex;
      padding: 8px;
      p {
        margin-right: 20px;
        span {
          color: blue;
        }
      }
    }
  }
}
</style>
